<template>
  <div class="app-container">
    <el-form class="formList" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch"
      label-width="80px">
      <el-row>

        <el-form-item label="栏目名称" prop="brandName">
          <el-input v-model="queryParams.brandName" placeholder="请输入栏目名称" clearable size="small"
            @keyup.enter.native="handleQuery" />
        </el-form-item>


        <el-form-item label="状态" prop="dataStatus">
          <el-select v-model="queryParams.dataStatus" placeholder="请选择状态" clearable size="small">
            <el-option v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue" />
          </el-select>
        </el-form-item>

        <el-form-item style="margin-left: 22px;">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>

      </el-row>
    </el-form>

    <el-row class="row-mbinfo">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['product:brand:add']">新增</el-button>
      </el-col>

      <el-col :span="1.5" style="margin-left: 10px">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['product:brand:remove']">删除</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <div class="divTable">
      <el-table border v-loading="loading" :data="brandList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />

        <el-table-column label="栏目名称" align="center" prop="brandName" />
        <el-table-column label="网址" align="center" prop="brandUrl" />

        <el-table-column label="状态" align="center" prop="dataStatus">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.dataStatus" active-value="1" inactive-value="0"
              @change="handleStatusChange(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="排序" align="center" prop="dataPriority" sortable />
        <el-table-column label="更新时间" align="center" prop="updateTime" width="180" sortable>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['product:brand:edit']">修改</el-button>
            <el-button v-if="scope.row.dataStatus == '0' " size="mini" type="text" icon="el-icon-delete"
              @click="handleDelete(scope.row)" v-hasPermi="['product:brand:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>
    <!-- 添加或修改品牌/系列对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px" append-to-body @keyup.enter.native="submitForm">
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="栏目名称" prop="brandName">
          <el-input v-model="form.brandName" placeholder="请输入栏目名称" class="select-normal" maxlength="20"
            show-word-limit />
        </el-form-item>

        <el-form-item label="排序" prop="dataPriority">
          <el-input v-model="form.dataPriority" placeholder="请输入排序"
            @input="form.dataPriority=dataPriorityInput(form.dataPriority)" class="select-normal" maxlength="5" />
        </el-form-item>
        <el-form-item label="网址" prop="brandUrl">
          <el-input v-model="form.brandUrl" placeholder="请输入网址" style="width: 400px;" maxlength="100"
            show-word-limit />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>



  </div>
</template>

<script>
  import {
    listBrand,
    getBrand,
    delBrand,
    addBrand,
    updateBrand,
    updateBrandStatus
  } from "@/api/product/brand";
  import imageArraySize from "@/components/ImageUpload/imageArraySize";
  export default {
    name: "Brand",
    components: {
      imageArraySize
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        //选中数组的名字
        names: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 品牌/系列表格数据
        brandList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 用于类别关联的分类显示
        dialogVisible: false,
        //常用状态数据字典
        statusOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          brandName: null,
          dataStatus: null,

        },
        // 表单参数
        form: {
          dialogImageUrl: '',
          dataPriority: "",
        },
        // 表单校验
        rules: {
          brandName: [{
            required: true,
            message: "栏目名称不能为空",
            trigger: "blur"
          }],
          dataPriority: [{
            required: true,
            message: "排序不能为空",
            trigger: "blur"
          }]
        },
        defaultProps: {
          children: 'children',
          label: 'label'
        }
      };
    },
    created() {
      this.getList();
      this.getDicts("sys_data_status").then(response => {
        this.statusOptions = response.data;
      });
    },
    methods: {
      dataPriorityInput(value) {
        let js = value + "";
        return js.replace(/^(0+)|[^\d]+/g, '')
      },
      /** 查询品牌/系列列表 */
      getList() {
        this.loading = true;
        listBrand(this.queryParams).then(response => {
          this.brandList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      handleRemove(file, fileList) {},
      handlePictureCardPreviewLogo(file) {
        this.logoUr = file.url;
        this.dialogVisible = true;
      },
      handlePictureCardPreviewImg(file) {
        this.imgUrl = file.url;
        this.dialogVisible = true;
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 关联的取消按钮
      cancelBrand() {
        this.openBrand = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          brandName: null,
          logoUrl: null,
          brandUrl: null,
          imgUrl: null,
          dataPriority: 10
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 品牌菜单 */
      getMenuTreeselect() {
        menuTreeselect().then(response => {
          this.menuOptions = response.data;
        });
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.brandUid)
        this.names = selection.map(item => item.brandName)
        this.single = selection.length !== 1
        this.multiple = !selection.length
        //用于禁用删除
        if (this.multiple == false) {
          for (var i = 0; i < selection.length; i++) {
            var element = selection[i];
            if ("1" === element.dataStatus) {
              this.multiple = true;
              break;
            }
          }
        }
      },
      // 状态修改
      handleStatusChange(row) {
        let text = row.dataStatus === "0" ? "无效" : "有效";
        this.$confirm('确定要把栏目名称为"' + row.brandName + '"设为' + text + '吗？', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return updateBrandStatus(row.brandUid, row.dataStatus);
        }).then(() => {
          this.msgSuccess("操作成功");
          this.getList();
        }).catch(function() {
          row.dataStatus = row.dataStatus === "0" ? "1" : "0";
        });
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加栏目";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const brandUid = row.brandUid || this.ids
        getBrand(brandUid).then(response => {
          this.form = response.data;
          console.log(this.form)
          this.open = true;
          this.title = "修改栏目";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.brandUid != null) {
              updateBrand(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addBrand(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const brandUids = row.brandUid || this.ids;
        const names = row.brandName || this.names;
        this.$confirm('是否确认删除栏目名称为"' + names + '"的栏目?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delBrand(brandUids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
      },

      /** 导出按钮操作 */
      handleExport() {
        this.download('product/brand/export', {
          ...this.queryParams
        }, `商品品牌.xlsx`)
      }
    }
  };
</script>
<style lang="scss" scoped>
  .treeOne {
    display: inline-block;
    margin-right: 200px;
  }

  .treeTwo {
    display: inline-block;
  }

  .image-rows {
    width: 80px;
    height: auto;
  }
</style>
